import 'package:flutter/material.dart';

typedef ButtonBuilder = Widget Function(Color color);

class Button extends StatefulWidget {
  final Function? onTap;
  final Color color;
  final Color hoverColor;
  final ButtonBuilder builder;

  const Button(
      {super.key,
      this.onTap,
      this.color = Colors.white,
      this.hoverColor = Colors.white38,
      required this.builder});

  @override
  _ButtonState createState() => _ButtonState();
}

class _ButtonState extends State<Button> {
  bool isOn = false;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTapDown: (TapDownDetails details) {
        setState(() {
          isOn = true;
        });
      },
      onTapUp: (TapUpDetails upDetails){
        setState(() {
          isOn = false;
        });
      },
      onTapCancel: () {
        setState(() {
          isOn = false;
        });
      },
      onTap: widget.onTap as GestureTapCallback,
      behavior: HitTestBehavior.opaque,
      child: widget.builder(isOn ? widget.hoverColor : widget.color),
    );
  }
}
